package com.example.quartzdemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.quartzdemo.entity.QuartzTask;
import com.example.quartzdemo.entity.dto.QuartzTaskDto;
import com.example.quartzdemo.quartz.QuartzTaskService;
import com.example.quartzdemo.service.QuartzTaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/tasks")
public class QuartzTaskController {
    @Autowired
    QuartzTaskService svc;
    @Autowired
    QuartzTaskRepository quartzTaskRepository;

    @GetMapping("/list")
    public IPage<QuartzTask> page(@RequestParam(value = "taskId",required = false) String taskId,
                                  @RequestParam(value = "className",required = false) String className,
                                  @RequestParam(value = "methodName",required = false) String methodName,
                                  @RequestParam(value = "concurrent",required = false) Boolean concurrent,
                                  @RequestParam(value = "status",required = false) String status,
                                  @RequestParam(value = "page",defaultValue = "1") Integer page,
                                  @RequestParam(value = "size",defaultValue = "10") Integer size
    ){
        QuartzTaskDto quartzTaskDto=new QuartzTaskDto(taskId,null,className,methodName,status,concurrent);
        return quartzTaskRepository.page(quartzTaskDto,page,size);
    }

    @PostMapping("/add")
    public String add(@RequestBody QuartzTaskDto dto) throws Exception {
        String message=svc.add(dto);
        return message==null?"OK":message;
    }

    @PostMapping("/remove")
    public String remove(@RequestParam String taskId) throws Exception {
        svc.remove(taskId); return "OK";
    }

    @PostMapping("/pause")
    public String pause(@RequestParam String taskId) throws Exception {
        svc.pause(taskId); return "OK";
    }

    @PostMapping("/resume")
    public String resume(@RequestParam String taskId) throws Exception {
        svc.resume(taskId); return "OK";
    }
    @PostMapping("/update")
    public String update(@RequestBody QuartzTask quartzTask) throws Exception {
        svc.update(quartzTask);
        return "OK";
    }
}

